package com.dh.web.controller;


import com.dh.core.config.Constant;
import com.dh.core.security.RoleConstant;
import com.dh.entity.User;
import com.dh.entity.UserFlow;
import com.dh.entity.UserGood;
import com.dh.entity.vo.UserGoodVo;
import com.dh.service.UserGoodService;
import com.dh.service.UserService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 优秀党员controller
 */
@RequestMapping("/admin/userGood")
@Controller
public class UserGoodController {

    @Autowired
    private UserGoodService userGoodService;


    @Autowired
    private UserService userService;

    /**
     * 转到列表展示页
     *
     * @param model
     * @param page
     * @param nickname
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @RequiresUser
    public String show(Model model, @RequestParam(required = false, value = "page", defaultValue = "1") int page, String nickname) {

        Page<UserGoodVo> pagination =
            userGoodService.page(new PageRequest(page - 1, 100, Sort.Direction.DESC, "createTime"));
        model.addAttribute("pagination", pagination);
        return "admin/user-good/good_list";
    }

    /**
     * 转到添加页面
     *
     * @return
     */
    @RequestMapping("/new")
    @RequiresUser
    public String editNew(Model model) {
        List<User> users = userService.findAllDy();
        model.addAttribute("dys", users);
        return "admin/user-good/good_create";
    }



    /**
     * 添加优秀党员信息
     *
     * @param userGood
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @RequiresUser
    public String create(UserGood userGood, RedirectAttributes redirectAttributes) {

        try {
            userGoodService.create(userGood);
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("message", "遇到错误");
            return "redirect:/admin/userGood/new";
        }
        return "redirect:/admin/userGood/list";
    }

    /**
     * 删除一个优秀党员
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @RequiresRoles(value = {RoleConstant.ROLE_ADMIN})
    public
    @ResponseBody
    Map<String, Object> delete(@PathVariable Long id) {
        Map<String, Object> map = new HashMap<>();
        userGoodService.delete(id);
        map.put("success", true);
        return map;
    }


    /**
     * 转到编辑优秀党员
     *
     * @param userGoodId
     * @param model
     * @return
     */
    @RequestMapping(value = "/edit/{userGoodId}", method = RequestMethod.GET)
    @RequiresUser
    public String edit(@PathVariable Long userGoodId, Model model) {
        UserGood good = userGoodService.get(userGoodId);
        model.addAttribute("u", good);
        List<User> users = userService.findAllDy();
        model.addAttribute("dys", users);
        return "admin/user-good/good_update";
    }


    /**
     * 更新优秀党员信息
     *
     * @param id
     * @param userGood
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @RequiresRoles(value = {RoleConstant.ROLE_ADMIN})
    public String update(@PathVariable("id") long id, UserGood userGood, RedirectAttributes redirectAttributes) {

        try {
            userGood.setId(id);
            userGoodService.update(userGood);
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("message", "出现错误");
            return "redirect:/admin/userGood/edit/" + id;
        }
        return "redirect:/admin/userGood/list";
    }

}
